package com.cndw.kungfu.model.player;

import java.util.Map;

import com.cndw.kungfu.config.DiscipleConfig;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.model.disciple.DiscipleDower;
import com.cndw.kungfu.model.disciple.DiscipleFeature;
import com.cndw.kungfu.model.disciple.domain.Disciple;

public class FormulaDisciple {

	/**
	 * 返回徒弟整体对角色属性的加成.
	 * 
	 * @param disciple
	 * @param vocation
	 * @return
	 */
	public static int[] clcaFirstProperties(Disciple disciple, int vocation) {
		int[] addV = new int[15];
		switch (vocation) {
		case 1:
			addV[7] = disciple.getPhysical() * 7;// 血量 ，职业平衡,武者兑换血量增加1点，由6点改为7点。
			addV[8] = disciple.getWit() * 5;// 气力
			addV[5] = disciple.getPower() * 3;// 攻击
			addV[6] = disciple.getPhysical() * 3;// 防御
			addV[9] = disciple.getAgile() * 1;// 速度
			addV[1] = disciple.getPower() * 1;// 命中
			addV[2] = disciple.getAgile() * 1;// 闪避
			addV[3] = disciple.getWit() * 1;// 暴击
			addV[4] = disciple.getPhysical() * 12 / 10;// 韧性
			break;
		case 2:
			addV[7] = disciple.getPhysical() * 5;
			addV[8] = disciple.getWit() * 5;
			addV[5] = disciple.getWit() * 3;
			addV[6] = disciple.getPhysical() * 3;
			addV[9] = disciple.getAgile() * 1;
			addV[1] = disciple.getPower() * 2;
			addV[2] = disciple.getAgile() * 1;
			addV[3] = disciple.getWit() * 1;
			addV[4] = disciple.getPhysical() * 12 / 10;
			break;
		case 3:
			addV[7] = disciple.getPhysical() * 6;
			addV[8] = disciple.getWit() * 5;
			addV[5] = disciple.getAgile() * 3;
			addV[6] = disciple.getPhysical() * 3;
			addV[9] = disciple.getAgile() * 1;
			addV[1] = disciple.getPower() * 2;
			addV[2] = disciple.getAgile() * 1;
			addV[3] = disciple.getWit() * 1;
			addV[4] = disciple.getPhysical() * 12 / 10;
			break;
		default:
			break;
		}

		return clacOneDisciple(disciple, addV);

	}

	/**
	 * 计算徒弟天赋和技能的属性加成
	 * 
	 * @param d
	 * @param dowerClacArr
	 * @return
	 */
	public static int[] clacOneDisciple(Disciple d, int[] dowerClacArr) {

		DiscipleFeature feature = DiscipleFeature.getById(d.getFeatureType());
		if (feature != null && feature.getAdd() != null && d.getFeatureLevel() > 0) {

			int add = feature.getAdd()[d.getFeatureLevel() - 1];
			switch (d.getFeatureType()) {
			case 1:
				dowerClacArr[1] += NumberProcesser.percentOnlyAddValue(d.getPower(), add);
				break;
			case 2:
				dowerClacArr[2] += NumberProcesser.percentOnlyAddValue(d.getAgile(), add);
				break;
			case 3:
				dowerClacArr[3] += NumberProcesser.percentOnlyAddValue(d.getWit(), add);
				break;
			case 4:
				dowerClacArr[4] += NumberProcesser.percentOnlyAddValue(d.getPhysical(), add);
				break;
			case 5:
				dowerClacArr[5] += NumberProcesser.percentOnlyAddValue(d.getPower(), add);
				break;
			case 6:
				dowerClacArr[6] += NumberProcesser.percentOnlyAddValue(d.getAgile(), add);
				break;
			case 7:
				dowerClacArr[7] += NumberProcesser.percentOnlyAddValue(d.getPhysical(), add);
				break;
			case 8:
				dowerClacArr[8] += NumberProcesser.percentOnlyAddValue(d.getWit(), add);
				break;
			default:
				break;
			}

		}

		int[] dowerAdd = new int[] { 0, 0, 0, 0 };
		/**
		 * 把天赋相加了.
		 */
		if (d.getDowerMap().isEmpty() == false) {
			for (Map.Entry<Integer, Integer> entry : d.getDowerMap().entrySet()) {
				int dowerId = entry.getKey();
				DiscipleDower dower = DiscipleDower.getById(dowerId);
				if (dower != null && dower.getAdd() != null) {
					int add = dower.getAdd()[entry.getValue() - 1];
					if (dowerId <= 14) {
						dowerClacArr[dowerId] += add;
					}
					if (dowerId >= 21 && dowerId <= 24) {
						dowerAdd[dowerId - 21] = add;
					}
				}
			}
		}

		if (dowerAdd[0] > 0) {
			dowerClacArr[1] += NumberProcesser.percentOnlyAddValue(d.getPower(), dowerAdd[0]);
		}
		if (dowerAdd[1] > 0) {
			dowerClacArr[2] += NumberProcesser.percentOnlyAddValue(d.getAgile(), dowerAdd[1]);
		}
		if (dowerAdd[2] > 0) {
			dowerClacArr[3] += NumberProcesser.percentOnlyAddValue(d.getWit(), dowerAdd[2]);
		}
		if (dowerAdd[3] > 0) {
			dowerClacArr[4] += NumberProcesser.percentOnlyAddValue(d.getPhysical(), dowerAdd[3]);
		}

		if (d.getGestSign() > 0) {
			int[] gestAdd = DiscipleConfig.gestAddMap.get(d.getGestSign());
			dowerClacArr[1] += gestAdd[5];
			dowerClacArr[2] += gestAdd[7];
			dowerClacArr[3] += gestAdd[6];
			dowerClacArr[4] += gestAdd[2];
			dowerClacArr[5] += gestAdd[9];
			dowerClacArr[6] += gestAdd[4];
			dowerClacArr[7] += gestAdd[1];
			dowerClacArr[8] += gestAdd[3];
			dowerClacArr[9] += gestAdd[8];
		}

		return dowerClacArr;

	}

	public static void getBasePropertys(Disciple disciples, int[] baseArr) {
		int percentSum = disciples.getPowerPercent() + disciples.getPhysicalPercent() + disciples.getAgilePercent() + disciples.getWitPercent();
		baseArr[0] = disciples.getGrowth() * disciples.getLevel() * disciples.getPowerPercent() / percentSum / 6;
		baseArr[1] = disciples.getGrowth() * disciples.getLevel() * disciples.getPhysicalPercent() / percentSum / 6;
		baseArr[2] = disciples.getGrowth() * disciples.getLevel() * disciples.getAgilePercent() / percentSum / 6;
		baseArr[3] = disciples.getGrowth() * disciples.getLevel() * disciples.getWitPercent() / percentSum / 6;

		baseArr[0] = baseArr[0] <= 0 ? 1 : baseArr[0];
		baseArr[1] = baseArr[1] <= 0 ? 1 : baseArr[1];
		baseArr[2] = baseArr[2] <= 0 ? 1 : baseArr[2];
		baseArr[3] = baseArr[3] <= 0 ? 1 : baseArr[3];
	}

	public static void getDevilPropertys(Disciple disciples, int[] devilArr) {
		int percentSum = disciples.getPowerPercent() + disciples.getPhysicalPercent() + disciples.getAgilePercent() + disciples.getWitPercent();
		if (disciples.getDevilTimes() > 0 && disciples.getDevilTimes() <= DiscipleConfig.devilMaxTimes) {
			int addValue = DiscipleConfig.devilAdd[disciples.getDevilTimes() - 1];
			int qualifiedType = disciples.getDiscipleMode().getQualifiedType();
			int[] xxx = new int[5];

			xxx[1] = addValue * disciples.getPowerPercent() / percentSum;
			xxx[2] = addValue * disciples.getPhysicalPercent() / percentSum;
			xxx[3] = addValue * disciples.getAgilePercent() / percentSum;
			xxx[4] = addValue * disciples.getWitPercent() / percentSum;
			xxx[qualifiedType] = 0;
			xxx[qualifiedType] = addValue - xxx[1] - xxx[2] - xxx[3] - xxx[4];
			devilArr[0] = xxx[1];
			devilArr[1] = xxx[2];
			devilArr[2] = xxx[3];
			devilArr[3] = xxx[4];
		}
	}

	public static void getDowerAddSelf(Disciple disciples, int[] dowerArr) {
		if (disciples.getDowerMap().isEmpty() == false) {// 天赋增加自身属性
			for (Map.Entry<Integer, Integer> entry : disciples.getDowerMap().entrySet()) {
				if (entry.getKey() < 15 || entry.getKey() > 18) {
					continue;
				}
				DiscipleDower discipleDower = DiscipleDower.getById(entry.getKey());
				if (discipleDower.getAdd() == null) {
					continue;
				}
				int addValue = discipleDower.getAdd()[entry.getValue() - 1];
				switch (entry.getKey()) {
				case 15:
					dowerArr[0] += (disciples.getPower() * 100 / (100 + addValue)) * addValue / 100;
					break;
				case 16:
					dowerArr[1] += (disciples.getPhysical() * 100 / (100 + addValue)) * addValue / 100;
					break;
				case 17:
					dowerArr[2] += (disciples.getAgile() * 100 / (100 + addValue)) * addValue / 100;
					break;
				case 18:
					dowerArr[3] += (disciples.getWit() / (100 + addValue)) * addValue;
					break;
				default:
					break;
				}
			}
		}
	}

}
